$(function() {
	$('.user-cell').live('click', function() {
		transfer.addUser($(this).attr("uid"),$(this));
		transfer.getChildren(transfer._level);
	});
	
	transfer.getUsersByDepartment();

	$("#c_form").submit(function() {
		var form_new_department = $("#form_new_department").val();
		var transfer_uids = $("#transfer_uids").val();
		var form_note = $("#form_note").val();
		var effective_time = $("#effective_time").val();
		var error = 0;
		if(form_new_department==0) {
			error = 2;
		} else if(transfer_uids == '') {
			error = 3;
		} else if(form_note == '') {
			error = 1;
		} else if(effective_time == '') {
			error = 4;
		}
		if(error != 0) {
			transfer._showError(error);
			return false;
		} else {
			return true;
		}
	});
	
	
	
});


$(function(){
	
	$('#search_transfer').live('click', function() {
		transfer.searchTransfer('page');
	});	
	$('#export_transfer').live('click', function() {
		transfer.searchTransfer('csv');
	});	
});



var transfer = (function(){
 
	var _count = 0;
	var _uids = new Array();
	var _level = 1; //当前的level
	
	var getUsersByDepartment = function(){
		_uids = new Array();
		var did = $("#userDepartment").val();
		if(did) {
			$.ajax({
				type: "GET",
				url: _siteUrl+"/UserDepartment/GetUsersByDepartment/",
				data: "id="+did,
				dataType: 'json',
				success: function(data){
					var _html = '';
					if(data.result.users)
					{
						$.each(data.result.users,function(value){
							_html += '<span class="user-cell" uid="'+this.unique_id+'">'+this.c_username+'</span>';	
						})
						$("#department-user-div").html(_html);
						_html = '';
						//alert(data.result.old_department.length);
						var i = 1;
						$.each(data.result.old_department,function(value){
							if(i==2) return true;
							_html += this.name;
							//if(i<data.result.old_department.length)
								//_html += '<img src="'+_siteUrl+'/global/img/icons/arrow_right.gif" />';
							i++;
						});
						$("#old_department").html(_html);
					}
				}
			});
		}
		//原部门信息
		
		transfer.clearFrom();
		$("#form_old_department").val(did);
		
	};
	
	var getUserInfo = function(uid) {
		$.ajax({
			type: "GET",
			url: _siteUrl+"/HecUser/GetUserInfo/",
			data: "uid="+uid,
			dataType: 'json',
			success: function(data){
				$("#form_c_username").val(data.result.c_username);
				$("#form_unique_id").val(data.result.unique_id);
				$("#form_username").val(data.result.username);
				$("#form_entry_point").val(data.result.entry_point);
				_html = '';
				$.each(data.result.old_department,function(value){
					_html += this.name;
				});
				$("#old_department").html(_html);
			}
		});
	}
	
	var addUser = function(uid,obj) {
		if(_searchArray(uid,_uids)) {
			//已经存在 反选
			obj.removeClass("bg_red");
			_delArray(uid,_uids);
			
		} else {
			obj.addClass("bg_red");
			//不存在 添加进数组
			_uids.push(uid);
		}
		$("#transfer_uids").val(_uids);
	}
	
	var _searchArray = function(key,arr) {
		for(var i = 0 ; i < _uids.length ; ++i) {
			if(arr[i] == key) {
				return true;	
			}
		}
		return false;
	}
	
	var _delArray = function(key,arr) {
		return arr.splice(arr.indexOf(key),1);
	}
	
	var getChildren = function(level) {
		$("#transfer_process").html("");
		var oid = $("#form_old_department").val();
		var did = $("#level_"+level).val();
		//删除比level级别高的select
		//alert(Number(level)+1);
		$("#level_"+(Number(level)+1)).remove();
		$("#level_"+(Number(level)+2)).remove();
		$("#level_"+(Number(level)+3)).remove();
		_level = level;
		level++;
		$("#level_"+level).remove();
		if(did) {
			$.ajax({
				type: "GET",
				url: _siteUrl+"/Transfer/GetDepartmentChildren/",
				data: "id="+did+"&oid="+oid+"&uids="+_uids,
				dataType: 'json',
				success: function(data){
					if(data.result.children!='') {
						var _html = '<select id="level_'+level+'" class="c_select" onchange="transfer.getChildren('+level+')" ><option value="0">--- 请选择 ---</option>';
						$.each(data.result.children,function(){
							_html += '<option value="'+this.id+'">'+this.name+'</option>';	
						})
						_html += '</select>';
						$("#new_department").append(_html);
					}
					if(data.result.process!='') {
						_html = '';
						var i = 1;
						$.each(data.result.process,function(){
							_html += this;
							if(i<data.result.process.length)
								_html += '<img src="'+_siteUrl+'/global/img/icons/arrow_right.gif" />';
							i++;	
						})
						
						$("#transfer_process").html(_html);
					}
				}
			});
		}
		$("#form_new_department").val(did);
		//alert(_level);
	}
	
	
	var searchTransfer = function(type) {
		var s_username = $('#s_username').val();
		var s_status = $('#s_status').val();
		var s_o_d = $('#s_o_d').val();
		var s_n_d = $('#s_n_d').val();
		var s_t_start = $('#s_t_start').val();
		var s_t_end = $('#s_t_end').val();
		
		var url = _siteUrl+'transfer/index?search[s_username]='+encodeURI(s_username)+'&search[s_status]='+s_status+'&search[s_o_d]='+s_o_d+'&search[s_n_d]='+s_n_d+'&search[s_t_start]='+s_t_start+'&search[s_t_end]='+s_t_end+'&search[type]='+type;
	
		//if(searchtype=='csv') {
		//	
		//} else {
			//document.location.href = url;
		//}
		if(type=='csv') {
			window.open(url); 	
		} else if(type=='page'){
			document.location.href = url;
		}
	}
	
	
	var clearFrom = function () {
		$("#form_new_department").val("0");
		$("#transfer_process").html("");
		$("#old_department").html("");
		$("#form_old_department").val("");
		$("#transfer_uids").val("");
		$("#level_2").remove();
		$("#level_3").remove();
		$("#level_4").remove();
		$("#level_1").val(0);
	}
	
	var _showError = function (e) {
		if(e == 1) {
			$("#notice").css("display","block");
			$("#notice").html("请填写调动说明！");
		} else if (e == 2) {
			$("#notice").css("display","block");
			$("#notice").html("请选择新部门！");
		} else if (e == 3) {
			$("#notice").css("display","block");
			$("#notice").html("请选择调动的元素！");
		} else if (e == 4) {
			$("#notice").css("display","block");
			$("#notice").html("请填写生效时间！");
		} 
	}
	 
	return {
		getUsersByDepartment : getUsersByDepartment,
		getUserInfo : getUserInfo,
		getChildren : getChildren,
		addUser : addUser,
		clearFrom : clearFrom,
		searchTransfer : searchTransfer,
		_uids : _uids,
		_showError : _showError,
		_level : _level
	};
 
})();